package com.uwlaser.modules.security.security;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 免Token认证过滤器
 */
public class DismissTokenFilter extends OncePerRequestFilter {

    private static final String FILTER_APPLIED = "__spring_security_filterSecurityInterceptor_filterApplied";

    private static List<String> DISMISS_TOKEN_URL = new ArrayList<>();

    public DismissTokenFilter(){

    }
    public DismissTokenFilter(List<String> age){
        DISMISS_TOKEN_URL.addAll(age);
    }
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        for (String url : DISMISS_TOKEN_URL) {
            if(requestURI.indexOf(requestURI) != -1){
                request.setAttribute(FILTER_APPLIED,true);
                break;
            }
        }
        filterChain.doFilter(request,response);
    }
}
